var operationStatus = "login";

function login() {
    clearMessage();
    username = $("#username").val();
    password = $("#passwd").val();

    if (!checkInputDataCorrect(username, password)) {
        return;
    }

    encPassword = encryptPassword(password);
    postData = {
        "user_name": username,
        "password": encPassword
    };

    sendLogin(postData);
}

function sendLogin(postData) {
    $.ajax({
        url: "/api/v1/login",
        type: "post",
        dataType: "json",
        data: JSON.stringify(postData),
        success: (result) => {
            console.log("获取到数据：" + JSON.stringify(result));
            resultCode = result.code;
            switch(resultCode) {
                case 1:
                    showMessage("好啦");
                    storeToken(result.data.access_token);
                    break;
                case 2:
                    showMessage("请求的数据不对哦");
                    break;
                case 3:
                    showMessage("密码或用户名不正确...");
                    break;
                case -1:
                    showMessage("哦豁，有个家伙写BUG了");
                    break;
                default :
                    showMessage("出现了不知道什么原因的错误，应给是服务器那边的锅")
            }
        },
        error: (msg, status) => {
            console.log(msg.status);
            showMessage("登录时发生错误：" + status + " " + msg.status);
        }
    })
}

function register() {
    clearMessage();
    username = $("#username").val();
    password = $("#passwd").val();

    if (!checkInputDataCorrect(username, password)) {
        return;
    }

    encPassword = encryptPassword(password);
    postData = {
        "user_name": username,
        "password": encPassword
    };

    $.ajax({
        url: "/api/v1/register",
        type: "post",
        dataType: "json",
        data: JSON.stringify(postData),
        success: (result) => {
            console.log("获取到数据：" + JSON.stringify(result));
            resultCode = result.code;
            switch(resultCode) {
                case 1:
                    showMessage("好啦");
                    storeToken(result.data.access_token);
                    break;
                case 2:
                    showMessage("请求的数据不对哦");
                    break;
                case 4:
                    showMessage("用户名已经被别人用啦");
                    break;
                case -1:
                    showMessage("哦豁，有个家伙写BUG了");
                    break;
                default :
                    showMessage("出现了不知道什么原因的错误，应给是服务器那边的锅")
            }
        },
        error: (msg, status) => {
            console.log(msg.status);
            showMessage("注册时发生错误：" + status + " " + msg.status);
        }
    })
}

function jumpToFillInformation() {
    
}